package com.ambow.shop.listener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.ambow.shop.domain.ProductCategory;
import com.ambow.shop.domain.ShoppingBasket;
import com.ambow.shop.domain.ShoppingBasketItem;
import com.ambow.shop.service.ProductCategoryService;
import com.ambow.shop.service.impl.ProductCategoryServiceImpl;

/**
 * 加载数据。
 *
 */
@WebListener
public class LoadDataListener implements ServletContextListener, HttpSessionListener {

	public LoadDataListener() {
		// TODO Auto-generated constructor stub
	}

	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
	}

	public void contextInitialized(ServletContextEvent sce) {
		ProductCategoryService categoryService = new ProductCategoryServiceImpl();
		List<ProductCategory> categories = categoryService.getProductCategories();
		sce.getServletContext().setAttribute("categories", categories);
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		// 当创建session时，就去初始化购物车信息，并放入session；
		ShoppingBasket basket = new ShoppingBasket(0, new Date(), new ArrayList<ShoppingBasketItem>(), null);
		se.getSession().setAttribute("basket", basket);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {

	}

}
